package com.zzty.txj.controller;


import com.zzty.txj.dao.UserMapper;
import com.zzty.txj.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping

public class UserController {
    @Autowired
    UserMapper userDao;

    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public String index(){
        return "HelloWord";
    }

    @RequestMapping("/getUserById/{id}")
    public User getUserById(@PathVariable("id") Integer id){
        return userDao.getUserById(id);
    }

    @RequestMapping("/user/check/{name}")
    public Map<String,String> getUserByName(@PathVariable("name") String name){
        Map<String,String> results=new HashMap<String,String>();
        User user=userDao.getUserByName(name);
        if(user==null){
            results.put("result","no user found");
        }else{
            results.put("result","success");
        }
        return results;
    }

    @RequestMapping("/user/getUserInfo")
   public User getUserInfo(HttpServletRequest request){
        System.out.println("session 2"+request.getSession().hashCode());
        User user=(User)request.getSession().getAttribute("user");
        System.out.println("user::"+user);
        return user;
   }
}
